package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aries.ui.view.radius.RadiusRadioButton;
import com.aries.ui.view.radius.RadiusTextView;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.ChooseDateModel;
import com.chexiehui.jc.mvp.car.NewCarListCovenant;
import com.chexiehui.jc.mvp.car.NewCarListPresenter;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.widget.LoadingLayout;
import com.nex3z.flowlayout.FlowLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 新车发布列表
 */
public class NewCarListActivity extends BaseMvpActivity<NewCarListPresenter> implements NewCarListCovenant.View, View.OnClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.fl_follow_tip)
    FlowLayout flFollowTip;
    private List<CarSeriesInfo> listData;
    private CommonAdapter<CarSeriesInfo> listAdapter;
    private List<ChooseDateModel> dateModelList;
    private int chooseIndex;

    @Override
    protected void beforeSetView() {
        dateModelList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -i);
            dateModelList.add(new ChooseDateModel(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
        }
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("新车发布")
                .setDividerVisible(false);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_new_car_list;
    }

    @Override
    protected void initView(Bundle var1) {
        loadingLayout.setStatus(LoadingLayout.Loading);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        refreshLayout.setEnableScrollContentWhenRefreshed(true);
        loadingLayout.setOnReloadListener(v -> {
            refreshLayout.finishRefresh(false);
            loadingLayout.setStatus(LoadingLayout.Loading);
            mvpPresenter.getList();
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            if (loadingLayout.getStatus() == LoadingLayout.Loading) {
                refreshLayout.finishRefresh(false);
                return;
            }
            mvpPresenter.getList();
        });
        recycler.setLayoutManager(new LinearLayoutManager(mContext));

        listData = new ArrayList<>();
        listAdapter = new CommonAdapter<CarSeriesInfo>(mContext, R.layout.item_new_car, listData) {
            @Override
            protected void convert(ViewHolder holder, CarSeriesInfo item, int position) {
                GlideUtil.loadImageViewCrop(mContext, item.getImg(), holder.getView(R.id.iv_item_image),
                        R.drawable.shape_square, R.drawable.shape_square);
                holder.setText(R.id.tv_item_name, item.getName());
                holder.setText(R.id.tv_item_price, item.getPriceQi());
                holder.setText(R.id.tv_item_time, item.getShangShiTime());
            }
        };
        listAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (NoDoubleClickUtils.isDoubleClick()) return;
                startActivity(CarSeriesDetailsActivity.class,
                        new BundleBuilder().putString("seriesId", listData.get(position).getId())
                                .create());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(listAdapter);
        setMonthTag();
    }

    private void setMonthTag(){
        flFollowTip.removeAllViews();
        for (int i = 0; i < dateModelList.size(); i++) {
            RadiusRadioButton radiusTextView = (RadiusRadioButton) View.inflate(mContext, R.layout.item_only_car_type3, null);
            radiusTextView.setTag(i);
            radiusTextView.setText(dateModelList.get(i).getMonth() + "月");
            radiusTextView.setOnClickListener(this);
            radiusTextView.setSelected(chooseIndex == i);
            flFollowTip.addView(radiusTextView);
        }
        loadingLayout.setStatus(LoadingLayout.Loading);
        refreshLayout.finishRefresh(false);
        mvpPresenter.getList();
    }

    @Override
    protected NewCarListPresenter createPresenter() {
        return new NewCarListPresenter(this);
    }

    @Override
    public void onGetListSuccess(BaseModel<List<CarSeriesInfo>> successResult) {
        if (loadingLayout.getStatus() != LoadingLayout.Success) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh(true);
        }
        listAdapter.getDatas().clear();
        listAdapter.getDatas().addAll(successResult.getData());
        listAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetListFailure(BaseModel<Object> failureResult) {
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            if (refreshLayout.getState() == RefreshState.Refreshing) {
                refreshLayout.finishRefresh(true);
            }
            listAdapter.getDatas().clear();
            listAdapter.notifyDataSetChanged();
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText(failureResult.getResultInfo());
        } else {
            if (refreshLayout.getState() == RefreshState.Refreshing) {
                refreshLayout.finishRefresh(false);
            }
            loadingLayout.setStatus(LoadingLayout.Error);
            loadingLayout.setErrorText(failureResult.getResultInfo());
        }
    }

    @Override
    public String getDateStr() {
        return dateModelList.get(chooseIndex).getYearMonth();
    }

    @Override
    public void onClick(View v) {
        if(v.getTag() != null){
            Integer i = (Integer) v.getTag();
            chooseIndex = i;
            setMonthTag();
        }
    }
}
